<template>
    <div id="orderRtx"
         v-loading="loading">
        <v-tableTitle :btnConfig="btnConfig"
                      :tableSelectData="tableSelectData"
                      :title="$t('config.expressSetting')"
                      @del="del"
                      @newkd="newkd"
                      @importordsx="importordsx"
                      @exportordsx="exportordsx"
                      @runjob="runjob"></v-tableTitle>
        <v-table :pageSize="pageSize"
                 :page="page"
                 :tableConfig="tableConfig"
                 :tableData="tableData"
                 :total="total"
                 @findData="getTableData"
                 @getTableData="getTableData"
                 @searhData="searhData"
                 @selectData="selectData"
                 @setdialog="setdialog"
                 @upData="getTableData"
                 ref="table"></v-table>
    </div>
</template>

<script type="text/ecmascript-6">
import tableTitle from "commons/TableTitle";
import table from "commons/Table";
import baseMixin from "@/mixins/BaseMixin";
import config from "./Config";

export default {
    name: "kdfreightkd",
    mixins: [baseMixin, config],
    data() {
        return {
            loading: false,
            tableConfig: [
                {
                    label: this.$t('config.feeCode'),
                    prop: "kdfreightkey",
                    input: "input",
                },
                {
                    label: this.$t('config.feeName'),
                    prop: "kdfreightdescr",
                    input: "input",
                },
                {
                    label: this.$t('config.carrierCode'),
                    prop: "carriercode",
                    input: "select",
                    options: this.getOptions("querycarriercodeDrowDown")
                },
                {
                    label: this.$t('config.product'),
                    prop: "product",
                    input: "select",
                    options: this.getOptions("queryProductDrowDown")
                },
                {
                    label: this.$t('config.foam'),
                    prop: "foam",
                    input: "input",
                },
                {
                    label: this.$t('common.status'),
                    prop: "status",
                    input: "select",
                    options: this.getOptions("querycodeLkup/RTXSTARTSTOP")
                },
                {
                    label: this.$t('config.startProvince'),
                    prop: "startstate",
                    input: "input",
                },
                {
                    label: this.$t('config.startCity'),
                    prop: "startcity",
                    input: "input",
                },
                {
                    label: this.$t('config.endProvince'),
                    prop: "endstate",
                    input: "input",
                },
                {
                    label: this.$t('config.endCity'),
                    prop: "endcity",
                    input: "input",
                },
                {
                    label: this.$t('config.pattern'),
                    prop: "pattern",
                    input: "input",
                },
                {
                    label: this.$t('app.addDate'),
                    prop: "adddate",
                    input: "date",
                    disabled: true
                },
                {
                    label: this.$t('app.addName'),
                    prop: "addwho",
                    input: "input",
                }
            ],
            tableData: [],
            pageSize: 10,
            page: 1,
            total: 10,
            tableSelectData: [],
            tableSearhData: {},
        };
    },
    computed: {
        btnConfig() {
            return [
                {label: this.$t('common.create'), certigier: !this.btnPermNameList.includes('WM031401'), invalid: "newkd"},
                {label: this.$t('common.delete'), certigier: !this.btnPermNameList.includes('WM031401'), disabled: true, invalid: "del"},
                {label: this.$t('common.import'), certigier: !this.btnPermNameList.includes('WM031401'), invalid: "importordsx"},
                {label: this.$t('common.export'), certigier: this.exportstatus||!this.btnPermNameList.includes('WM031401'), disabled: true, invalid: "exportordsx"},
                {label: this.$t('config.runConputedJob'), certigier: !this.btnPermNameList.includes('WM031401'), invalid: "runjob"},
            ]
        }
    },
    created() {
    },
    methods: {
        //测试用按钮
        async runjob() {
            try {
                let res = await this.$api.get(`/kdfreightCount`);
                
                    this.$message.success(this.$t('msg.msg6'));
            } catch (e) {
                this.$message.error(e);
            } finally {
                
            }
        },
        //新建
        async newkd() {
            let whseid = this.$store.state.wms;
            let opeuser = this.$Cookies.get('user');
            let name = "RTXKDFREIGHT";
            try {
                let res = await this.$api.get(`/getHttpUrl/${name}`);
                
                    let url = `${res.bean}?whseid=${whseid}&users=${opeuser}&typeClick=newType`;
                    // let url = `http://localhost:8086/rtxext/kdfreight?whseid=wmwhse1&users=wangc&typeClick=newType`;
                    this.$router.push({name: "iframe", params: {url: url, title: "新建快递费设置"}});
                
            } catch (e) {
                this.$message.error(e);
            }
        },
        //导入
        async importordsx() {
            let whseid = this.$store.state.wms;
            let opeuser = this.$Cookies.get('user');
            let name = "RTXKDFREIGHTIMPORT";
            try {
                let res = await this.$api.get(`/getHttpUrl/${name}`);
                
                    let url = `${res.bean}?whseid=${whseid}&users=${opeuser}`;
                    // let url = `http://localhost:8086/rtxext/kdfreightimport?whseid=wmwhse1&users=wangc`;
                    this.$router.push({name: "iframe", params: {url: url, title: "导入快递费设置"}});
                
            } catch (e) {
                this.$message.error(e);
            }
        },
        //删除
        async del(data) {
            try {
                let res = await this.$api.post(`/deleteRtxKdfreight`, data);
                
                    this.$message.success(res.returnMgr);
                    let page = sessionStorage.getItem('page');
                    this.getTableData(page, this.tableSearhData);
                
            } catch (e) {
                this.$message.error(e);
            } finally {
                
            }
        },

        //导出
        async exportordsx(data) {
            try {
                let res = await this.$api.post('/exportKdfreightKd', data, {responseType: 'blob'})
                if (window.navigator.msSaveBlob) {
                    var blobObject = new Blob([res.data]);
                    window.navigator.msSaveBlob(blobObject, this.$t('config.express')+'.xls');
                } else {
                    var blob = new Blob([res.data], {type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=utf-8'});
                    var filename = this.$t('config.express') + '.xls';
                    var downloadElement = document.createElement('a');
                    var href = window.URL.createObjectURL(blob); //创建下载的链接
                    downloadElement.style.display = 'none';
                    downloadElement.href = href;
                    downloadElement.download = filename; //下载后文件名
                    document.body.appendChild(downloadElement);
                    downloadElement.click(); //点击下载
                    document.body.removeChild(downloadElement); //下载完成移除元素
                    window.URL.revokeObjectURL(href); //释放掉blob对象
                }
            } catch (e) {
                this.$message.error(e);
            } finally {
                
            }
        },

        //选中的内容
        selectData(data) {
            this.tableSelectData = data;
        },
        async getTableData(page, data) {
            try {
                this.loading = true;
                let res = await this.$api.post(`/queryRtxKdfreight/${page}`, data);
                
                    this.tableData = res.page.list;
                    this.pageSize = res.page.pageSize;
                    this.page = res.page.curpage;
                    this.total = res.page.total;
                
            } catch (e) {
                this.$message.error(error);
            } finally {
                this.loading = false;
            }
        },
        //搜索的值
        searhData(data, up) {
            if (!up) {
                this.tableSearhData = data;
            } else {
                this.tableSearhData = data;
                this.getTableData(1, this.tableSearhData);
            }
        },
        //设置弹框
        async setdialog(data) {
            this.$store.commit("setIframeSrc", {data: [], type: "array"});
            let whseid = this.$store.state.wms;
            let opeuser = this.$Cookies.get('user');
            let kdfreightkey = data.kdfreightkey;
            let name = "RTXKDFREIGHT";
            try {
                let res = await this.$api.get(`/getHttpUrl/${name}`);
                
                    let url = `${res.bean}?whseid=${whseid}&users=${opeuser}&kdfreightkey=${kdfreightkey}`;
                    // let url = `http://localhost:8086/rtxext/kdfreight?whseid=wmwhse1&users=wangc&kdfreightkey=${kdfreightkey}`;
                    this.$router.push({name: "iframe", params: {url: url, title: "快递费详情"}});
                
            } catch (e) {
                this.$message.error(e);
            }


        },
    },
    mounted() {
        // ;
        this.$store.commit("setSkip", {});
        this.$store.commit("setRouteId", '');
        this.$store.commit("setNewFlag", false);
        this.$store.commit("setRouteData", {});
        this.$store.commit("setTemporary", "");
        this.getTableData(1, {});
    },
    watch: {
        tableSelectData(data) {
            if (this.permissionStatus) {
                if (data.length > 0) {
                    this.deleteStatus = this.downloadStatus = false;
                    this.allocationStatus = this.fafangStatus = false
                    this.deliverStatus = this.allPickStatus = this.allShipStatus = false;
                } else {
                    this.deleteStatus = this.downloadStatus = true;
                    this.allocationStatus = this.fafangStatus = true;
                    this.deliverStatus = this.allPickStatus = this.allShipStatus = true;
                }
            }
        }
    },
    components: {
        "v-tableTitle": tableTitle,
        "v-table": table,
    }
};
</script>

<style scoped>
#orderRtx {
    width: 100%;
    height: 99%;
    padding-top: 6px;
}
</style>
